layui.use(['table','layer','form', 'layedit','jquery'], function(){
  var table = layui.table
      ,layer = layui.layer
      ,form = layui.form
      ,$ = layui.jquery
      ,element = layui.element;

  table.render({
    elem: '#guestTable'
    ,id: 'guestTableID'
    ,url:'../guest/findGuestsByKeys.json'
    ,skin: 'line'
    ,even: true //开启隔行背景
    ,response: {
      statusName: 'code'
      ,statusCode: '100012'
      ,msgName: 'msg'
      ,countName: 'total'
      ,dataName: 'data'
    }
    ,toolbar: '#guestToolBar'
    ,title: '用户数据表'
    ,cols: [[
      {field:'guest_id', title:'编号', width:80, fixed: 'left', sort: true,unresize: true, width:100}
      ,{field:'head_picture', title:'头像',templet:'#imgtmp'}
      ,{field:'guest_name', title:'用户名'}
      ,{field:'guest_name', title:'粉丝'}
      ,{field:'country', title:'国家'}
      ,{field:'balance', title:'余额'}
      ,{field:'experience', title:'购买次数'}
      ,{field:'ip', title:'累计提现'}
      ,{field:'logins', title:'登积分'}
      ,{field:'create_time', title:'首次访问'}
      ,{field:'join_time', title:'最近访问'}
      ,{field:'update_time', title:'更新时间'}
      ,{fixed: 'right', title:'操作',toolbar: '#guestBar',fixed:'right',unresize: true,width:100}
    ]]
    ,page: true
  });

  /*删除会员信息*/
  function delGuestByUnionID(unionID){
    $.ajax({
      url:'../guest/findGuestsByKeys.json',
      type:'GET',
      async:true,
      data:{
        unionID:unionID
      },
      timeout:5000,    //超时时间
      dataType:'json', //返回的数据格式：json/xml/html/script/jsonp/text
      success:function(res){
        /*layer.msg("删除数据成功！",{icon:1,time:1000,function{

          });*/
        return true;
      },
      error:function(res){
        layer.msg("新增字典类型出错了！");
        return false;
      },
      complete:function(){
        //刷新父页面数据
        location.reload();
      }
    });
  }
  //头工具栏事件
  table.on('toolbar(guestTable)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
      case 'getCheckData':
        var data = checkStatus.data;
        layer.alert(JSON.stringify(data));
        break;
      case 'getCheckLength':
        var data = checkStatus.data;
        layer.msg('选中了：'+ data.length + ' 个');
        break;
      case 'isAll':
        layer.msg(checkStatus.isAll ? '全选': '未全选');
        break;
    };
  });

  //监听行工具事件
  table.on('tool(guestTable)', function(obj){
    var data = obj.data;
    console.log(obj)
    if(obj.event === 'del'){
      //获取选中行数据
      layer.confirm('真的删除行么', function(index){
        obj.del();
        layer.close(index);
      });
    } else if(obj.event === 'detail'){
      layer.prompt({
        formType: 2
        ,value: data.email
      }, function(value, index){
        obj.update({
          email: value
        });
        layer.close(index);
      });
    }
  });

  //搜索按钮点击事件
  $("#guest_search_id").on('click',function(){
    //获取输入的值
    var guestName = $('#guestName').val();
    var currentSex = $('select[name="sex"] option:selected').text();
    if(currentSex=='选择或输入搜索'){
      currentSex = '';
    }
    console.log('guestName',guestName);
    console.log('currentSex',currentSex);
    table.reload('guestTableID', {
      method: 'get'
      ,where: {
        guestname:guestName,
        sex:currentSex
      }
      , page: {curr: 1}
    });

  });
  
  
  //搜索按钮点击事件
  /*$("#guest_search_id").on('click',function(){
    //获取输入的值
    var guestName = $('#guestName').val();
    var currentSex = $('select[name="sex"] option:selected').text();
    
    $.ajax({
      url:'../guest/findGuestsByKeys.json',
      type:'GET',
      async:true,
      data:{
        guestname:guestName,
        sex:currentSex
      },
      timeout:5000,    //超时时间
      dataType:'json', //返回的数据格式：json/xml/html/script/jsonp/text
      success:function(res){
        layer.msg("新增字典类型成功！");
        return true;
      },
      error:function(res){
        layer.msg("新增字典类型出错了！");
        return false;
      },
      complete:function(){
        //刷新父页面数据
        location.reload();
      }
    });
  });*/

});